﻿using System.Threading.Tasks;
using IrisMembership.DataLayer;
using IrisMembership.DomainClasses;
using IrisMembership.ServiceLayer;
using IrisMembership.Web.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;

namespace IrisMembership.Web.Controllers
{
    [RoutePrefix("role")]
    public class RoleController : Controller
    {
        private readonly IIrisDbContext _dbContext;
        private readonly IRoleService _roleService;

        public RoleController(IIrisDbContext dbContext, IRoleService roleService)
        {
            _dbContext = dbContext;
            _roleService = roleService;
        }

        public ActionResult Index()
        {
            return PartialView();
        }

        public ActionResult AddRole()
        {
            return PartialView();
        }


        public async Task<ActionResult> GetUserRoles()
        {
            var userId = this.GetUser().Information.UserId;

            var roles = await _roleService.GetRolesOfUserAsync(userId);

            var rolesList = new List<Role>();

            //loadTreeNode(roles, rolesList);
            var data =
                await
                    JsonConvert.SerializeObjectAsync(roles, Formatting.Indented,
                        new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return
                Content(data);
        }

        public async Task<ActionResult> MyAuthorizedRoles()
        {
            var userId = this.GetUser().Information.UserId;

            var roles = await _roleService.GetRolesOfUserExceptMainRoleAsync(userId);

            var rolesList = new List<Role>();

            //loadTreeNode(roles, rolesList);
            var data =
                await
                    JsonConvert.SerializeObjectAsync(roles, Formatting.Indented,
                        new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return
                Content(data);
        }

        [HttpPost]
        public async Task<ActionResult> AddRole(AddRoleModel roleModel)
        {
            _roleService.Add(new Role
            {
                Name = roleModel.Name,
                ParentRoleId = roleModel.ParentRoleId
            });
            await _dbContext.SaveChangesAsync();
            return Json("ok");
        }


        [Route("getuserroles/{userId:int}")]
        public async Task<ActionResult> GetUserRoles(int userId)
        {
            var roles = await _roleService.GetRolesOfUserAsync(userId);

            var data =
                await
                    JsonConvert.SerializeObjectAsync(roles.Select(x => new { Id = x.Id }), Formatting.Indented,
                        new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            return
                Content(data);
        }

    }
}